# -*- coding: utf-8 -*-
"""
@ Created on 2024-06-14 12:23
---------
@summary: 自定义异常类
---------
@author: Lsaiah
"""

from fastapi import HTTPException, status
from typing import Optional, Any, Dict


class CustomHTTPException(HTTPException):
    """自定义HTTP异常类"""
    
    def __init__(
        self,
        status_code: int,
        code: int,
        message: str,
        data: Optional[Any] = None,
        headers: Optional[Dict[str, str]] = None
    ):
        super().__init__(status_code=status_code, detail=None, headers=headers)
        self.code = code
        self.message = message
        self.data = data


class AuthenticationException(CustomHTTPException):
    """认证异常"""
    
    def __init__(self, message: str = "认证失败", code: int = 401):
        super().__init__(
            status_code=status.HTTP_401_UNAUTHORIZED,
            code=code,
            message=message,
            headers={"WWW-Authenticate": "Bearer"}
        )


class AuthorizationException(CustomHTTPException):
    """授权异常"""
    
    def __init__(self, message: str = "权限不足", code: int = 403):
        super().__init__(
            status_code=status.HTTP_403_FORBIDDEN,
            code=code,
            message=message
        )


class ValidationException(CustomHTTPException):
    """验证异常"""
    
    def __init__(self, message: str = "数据验证失败", code: int = 400):
        super().__init__(
            status_code=status.HTTP_400_BAD_REQUEST,
            code=code,
            message=message
        )


class NotFoundException(CustomHTTPException):
    """资源不存在异常"""
    
    def __init__(self, message: str = "资源不存在", code: int = 404):
        super().__init__(
            status_code=status.HTTP_404_NOT_FOUND,
            code=code,
            message=message
        )


class BusinessException(CustomHTTPException):
    """业务异常"""
    
    def __init__(self, message: str = "业务处理失败", code: int = 400):
        super().__init__(
            status_code=status.HTTP_400_BAD_REQUEST,
            code=code,
            message=message
        )
